<template>
    <div class="address">
        <!-- <navback title = "地址管理" :left-arrow="true" left-text="返回" fixed></navback> -->
        <van-nav-bar title="地址管理" left-text="返回" left-arrow @click-left="onClickLeft" class="navback"  />
        <div>
            <van-address-list v-model="chosenAddressId" :list="list" default-tag-text="默认" @add="toNewAddress" @edit="editAddress" @click-item="toggleAddress" />

            <div v-if="list.length === 0">
                <van-empty image="error" description="没有地址数据!" />
            </div>
        </div>
    </div>
</template>

<script>
import navback from "../components/Navback.vue";
export default {
    name: "Address",
    data() {
        return {
            carlist: "",
            content: "",
            chosenAddressId: "1",
            list: [
                {
                    id: "1",
                    name: "坤坤",
                    tel: "13000000000",
                    address: "浙江省杭州市西湖区文三路 138 号东方通信大厦 7 楼 501 室",
                    isDefault: false,
                },
            ],
        };
    },
    created() {
        // 获取新增地址页面传来的content地址信息
        console.log(this.$route.query.content);
        if (this.$route.query.content !== undefined) {
            this.content = this.$route.query.content;
            console.log(this.content);

            this.list.unshift({
                id: "2",
                name: this.content.name,
                tel: this.content.tel,
                address: this.content.province + this.content.city + this.content.county + this.content.addressDetail,
                isDefault: this.content.isDefault,
            });
        }
        // 接受结算页面传来的carlist数据
        // if(this.$route.query.carlist!==undefined){

        //     let carlist = JSON.parse(this.$route.query.carlist)
        //     console.log(carlist);
        //     this.carlist = carlist
        //     console.log(this.carlist);
        // }
    },
    components: {
        navback,
    },
    methods: {
        onClickLeft() {
            this.$router.back();
        },
        // 新增地址
        toNewAddress() {
            this.$router.push({
                name: "Newaddress",
            });
        },
        // 编辑地址
        editAddress() {
            this.$router.push({ name: "Newaddress", query: { aid: "aaa" } });
        },
        // 切换地址显示到结算页面
        toggleAddress() {
            if (this.content !== "") {
                this.$router.push({
                    name: "Close",
                    query: {
                        addressInfo: JSON.stringify(this.content),
                    },
                    // query:{
                    //     carlist:JSON.stringify(this.carlist)
                    // }
                });
            } else {
                console.log(1111);
                this.$router.push({
                    name: "Close",
                    query: {
                        list: JSON.stringify(this.list[0]),
                    },
                });
            }
            // 将本地存储的Carlist传回给结算页面
        },
    },
};
</script>

<style lang="less" scoped>
.address {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #faf4f4;
    /deep/ .van-nav-bar__title {
        color: #656565;
        font-size: 18px;
    }

    /deep/ .van-nav-bar__text{
        color: #656565;
    }
    /deep/ .van-icon-arrow-left{

        color: #656565;
        }
}
</style>
